/* Copyright 2010 John L. Reilly

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

package com.riq;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.riq.entity.Icon;


@SuppressWarnings("serial")
public class AdminIconServlet extends HttpServlet {
  private static final Logger log = Logger.getLogger(Icon.class.getName());

  private Long deptId;
  
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    doGet(request, response);
  }
  
  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException { 

  // ---------------------------------------------------------------------------------------------
  //  HEADER: Icon
  // ---------------------------------------------------------------------------------------------

    Cookie[] cookie = request.getCookies();
    deptId = Long.valueOf(ServletUtilities.getCookieValue(cookie, "cvDeptId", "99999"));
    String action = request.getParameter("action"); 
    
  // ---------------------------------------------------------------------------------------------
  //  METHOD DIRECTORY: Icon
  // ---------------------------------------------------------------------------------------------

    if ("prepCreate".equalsIgnoreCase(action)) { 
      prepCreate(response, request); 
     } else if ("create".equalsIgnoreCase(action)) { 
      create(response, request); 
    } else if ("display".equalsIgnoreCase(action)) { 
      display(response, request);
    } else if ("edit".equalsIgnoreCase(action)) { 
      edit(response, request); 
    } else if ("set".equalsIgnoreCase(action)) { 
      set(response, request); 
    } else if ("delete".equalsIgnoreCase(action)) { 
      delete(response, request); }
  }

  // -----------------------------------------------------------------------------------------------
  //  METHODS
  // -----------------------------------------------------------------------------------------------

  // PREP CREATE Icon

  private void prepCreate(HttpServletResponse response, ServletRequest request) 
     throws ServletException, IOException {

		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/iconCreate.jsp?deptId=" + deptId.toString());
		dispatcher.forward(request, response);	
    
  }
    
    
    
  // CREATE Icon
  
  void create(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
  
    String padT = request.getParameter("padT");
    int padTint = new Integer(padT).intValue();
    
    String padB = request.getParameter("padB");
    int padBint = new Integer(padB).intValue();
    
    String padL = request.getParameter("padL");
    int padLint = new Integer(padL).intValue();
    
    String padR = request.getParameter("padR");
    int padRint = new Integer(padR).intValue();
    
    Icon i = new Icon (
            deptId,
            request.getParameter("shortName"),
            request.getParameter("longName"),
            request.getParameter("type"),
            request.getParameter("bcolor"),
            request.getParameter("fcolor"),
            padTint,
            padBint,
            padLint,
            padRint);
    
    pm.makePersistent(i);   
    pm.close();
    try
    {
      response.sendRedirect("department?action=display");
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }


  private void display(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long iconId = Long.valueOf(request.getParameter("iconId"));
    Icon i = pm.getObjectById(Icon.class, iconId);
    request.setAttribute("icon", i);
    
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/iconDisplay.jsp");
    try
    {
      dispatcher.forward(request, response);
    } catch (ServletException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }


  
  private void edit(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long iconId = Long.valueOf(request.getParameter("iconId"));

    Icon i = pm.getObjectById(Icon.class, iconId);	
    request.setAttribute("icon", i);
    
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/iconEdit.jsp");
    try
    {
      dispatcher.forward(request, response);
    } catch (ServletException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  
  
  private void set(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    
    // open object in order to set values passed from form
    Long iconId = Long.valueOf(request.getParameter("iconId"));
    Icon i = pm.getObjectById(Icon.class, iconId); 
    
    // cell padding between letters and box walls
    String padT = request.getParameter("padT");
    int padTint = new Integer(padT).intValue();
    String padB = request.getParameter("padB");
    int padBint = new Integer(padB).intValue();
    String padL = request.getParameter("padL");
    int padLint = new Integer(padL).intValue();
    String padR = request.getParameter("padR");
    int padRint = new Integer(padR).intValue();
    
    // set values
    i.setdeptId(Long.valueOf(request.getParameter("deptId")));
    i.setshortName(request.getParameter("shortName"));
    i.setlongName(request.getParameter("longName"));
    i.settype(request.getParameter("type"));
    i.setbcolor(request.getParameter("bcolor"));
    i.setfcolor(request.getParameter("fcolor"));
    i.setpadT(padTint);
    i.setpadB(padBint);
    i.setpadL(padLint);
    i.setpadR(padRint);
    pm.makePersistent(i);  
    pm.close();
    
    try
    {
      response.sendRedirect("icon?action=display&iconId=" + 
              i.getid().toString() + "&deptId=" + i.getdeptId().toString());
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  // DELETE Icon

  private void delete(HttpServletResponse response, ServletRequest request) {

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Long iconId = Long.valueOf(request.getParameter("iconId"));
    Icon i = pm.getObjectById(Icon.class, iconId);
    pm.deletePersistent(i);
    try
    {
      response.sendRedirect("department?action=display");
    } catch (IOException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }


 
}

